package com.quectel.core.module.parking.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.parking.dao.ParkingChargingSharingTimeDao;
import com.quectel.core.module.parking.dto.ParkingChargingSharingTimeDto;
import com.quectel.core.module.parking.entity.ParkingChargingSharingTimeEntity;
import com.quectel.core.module.parking.service.ParkingChargingSharingTimeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/05/11 10:26
 */
@DubboService
public class ParkingChargingSharingTimeServiceImpl extends ServiceImpl<ParkingChargingSharingTimeDao, ParkingChargingSharingTimeEntity> implements ParkingChargingSharingTimeService{

    private static final Logger LOGGER = LoggerFactory.getLogger(ParkingChargingSharingTimeServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARKING_CHARGING_SHARING_TIME_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARKING_CHARGING_SHARING_TIME_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<ParkingChargingSharingTimeDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARKING_CHARGING_SHARING_TIME_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private ParkingChargingSharingTimeDao parkingChargingSharingTimeDao;


    @Override
    public void saveBatch(List<ParkingChargingSharingTimeDto> list) {
        List<ParkingChargingSharingTimeEntity> parkingChargingSharingTimeEntities = CopyUtils.copyList(list, ParkingChargingSharingTimeEntity.class);
        for (ParkingChargingSharingTimeEntity parkingChargingSharingTimeEntity : parkingChargingSharingTimeEntities) {
            parkingChargingSharingTimeEntity.setId(Snowflake.nextId());
        }
        saveBatch(parkingChargingSharingTimeEntities);
    }

    @Override
    public List<ParkingChargingSharingTimeDto> selectByChargingId(Long chargingId) {
        List<ParkingChargingSharingTimeEntity> parkingChargingSharingTimeEntities = parkingChargingSharingTimeDao.selectList(new LambdaQueryWrapper<ParkingChargingSharingTimeEntity>()
                .eq(ParkingChargingSharingTimeEntity::getParkingChargingId, chargingId));
        List<ParkingChargingSharingTimeDto> parkingChargingSharingTimeDtos = CopyUtils.copyList(parkingChargingSharingTimeEntities, ParkingChargingSharingTimeDto.class);
        parkingChargingSharingTimeDtos.forEach(this::paddingField);
        return parkingChargingSharingTimeDtos;
    }

    @Override
    public ParkingChargingSharingTimeDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARKING_CHARGING_SHARING_TIME_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ParkingChargingSharingTimeDto selectById(Long id){
            ParkingChargingSharingTimeEntity entity = parkingChargingSharingTimeDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,ParkingChargingSharingTimeDto.class));
    }

    @Override
    public List<ParkingChargingSharingTimeDto> queryList(Map<String,Object> params){
        List<ParkingChargingSharingTimeEntity> list = parkingChargingSharingTimeDao.queryList(params);
        List<ParkingChargingSharingTimeDto> result =  CopyUtils.copyList(list,ParkingChargingSharingTimeDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private ParkingChargingSharingTimeDto paddingField(ParkingChargingSharingTimeDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return parkingChargingSharingTimeDao.queryTotal(params);
    }

    @Override
    public Long save(ParkingChargingSharingTimeDto dto){
        ParkingChargingSharingTimeEntity entity = CopyUtils.copyObj(dto,ParkingChargingSharingTimeEntity.class);
        entity.setId(Snowflake.nextId());
        parkingChargingSharingTimeDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ParkingChargingSharingTimeDto dto){
        ParkingChargingSharingTimeEntity entity = CopyUtils.copyObj(dto,ParkingChargingSharingTimeEntity.class);

        parkingChargingSharingTimeDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(ParkingChargingSharingTimeDto dto) {
       ParkingChargingSharingTimeEntity entity = CopyUtils.copyObj(dto,ParkingChargingSharingTimeEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, parkingChargingSharingTimeDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        parkingChargingSharingTimeDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
